package com.zq.ssmdemo.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zq.ssmdemo.mapper.EmployeesMapper;
import com.zq.ssmdemo.pojo.DtoUpdateLL;
import com.zq.ssmdemo.pojo.Employees;
import com.zq.ssmdemo.service.IEmployeesService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 员工信息表 服务实现类
 * </p>
 *
 * @author 张乔
 * @since 2024-06-09
 */
@Service
public class EmployeesServiceImpl implements IEmployeesService {

    @Resource
    private EmployeesMapper employeesMapper;


//    员工登录
    @Override
    public void login(String username, String password) {
       Employees employees = employeesMapper.selectOne(new LambdaQueryWrapper<Employees>()
               .eq(StringUtils.hasText(username),Employees::getUsername, username)
       );
        String md5Hex = DigestUtil.md5Hex(password);
        if (!employees.getPassword().equals(md5Hex)){
            throw new RuntimeException("密码错误");
        }
    }

    @Override
    public void register(Employees employees) {

        String md5Hex = DigestUtil.md5Hex(employees.getPassword());
        employees.setPassword(md5Hex);
        employeesMapper.insert(employees);

    }



    @Override
    public PageInfo<Employees> getAllEmployees() {


        PageHelper.startPage(1, 2);
        List<Employees> employees = employeesMapper.selectList(null);
        PageInfo<Employees> page = new PageInfo<>(employees);
        return page;
    }

    @Override
    public PageInfo<Employees> getPageEmployees(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Employees> employees = employeesMapper.selectList(null);

        // 格式化日期
        employees.forEach(employee -> {
            System.out.println("---------------------------------");

        employee.setDateOfS(employee.getDateOf().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))    ;
        employee.setHireDateS(employee.getHireDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))    ;
//       将图片格式化成base64并直接输出
        if(employee.getAvatar()!=null){
            String dataUrl = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(employee.getAvatar());
            employee.setBase64Avatar(dataUrl);
        }
        });
        System.out.println("循环结束");
        PageInfo<Employees> pageInfo = new PageInfo<>(employees);

        return pageInfo;
    }


    @Override
    public void update(DtoUpdateLL employees) {

employeesMapper.updateByIdsss(employees);
//        employeesMapper.updateByIdss(employees);
    }

    @Override
    public void delete(int id) {
employeesMapper.deleteByIdss(id);
    }

    @Override
    public void registersss(Employees employees) {
        String dateOfS = employees.getDateOfS();
        String hireDateS = employees.getHireDateS();
        if (dateOfS != null) {
            employees.setDateOf
                    (LocalDate.parse(dateOfS, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        if (hireDateS != null) {
            employees.setHireDate (LocalDate.parse(hireDateS, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        String md5Hex = DigestUtil.md5Hex(employees.getPassword());
        employees.setPassword(md5Hex);
        employeesMapper.insert(employees);
    }

}
